package com.stack.auto_parts.controller;

import com.stack.auto_parts.common.ApiResult;
import com.stack.auto_parts.entity.Product;
import com.stack.auto_parts.service.ProductService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Title: ProductController
 * @Author WUYU_MIN
 * @Package com.stack.auto_parts.controller
 * @Date 2024/7/10 10:31
 * @description:
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Resource
    private ProductService productService;

    // 查询所有商品
    @GetMapping("/selectAll")
    public ApiResult<List<Product>> selectAll(){
        List<Product> ProductList = productService.selectAll();
        return ApiResult.success(ProductList);
    }


    // 上架商品
    @PostMapping("/putaway")
    public ApiResult<Void> putaway(@RequestBody Product product){

        String result = productService.putaway(product);
        if(result != null)
            return ApiResult.fail(result);
        else
            return ApiResult.success();
    }

    // 下架商品
    @DeleteMapping("/deleteProduct")
    public ApiResult<Void> deleteProduct(String uuid){
        String result = productService.deleteProduct(uuid);
        if(result != null)
            return ApiResult.fail(result);
        else
            return ApiResult.success();
    }

    // 更新商品
    @PostMapping("/updateProduct")
    public ApiResult<Void> updateProduct(@RequestBody Product product){
        String result = productService.updateProduct(product);
        if(result != null)
            return ApiResult.fail(result);
        else
            return ApiResult.success();
    }

    // 根据条件查询商品
    @GetMapping("/selectByCondition")
    public ApiResult<List<Product>> listBycondition(String partName,String supplier){
        if(Objects.equals(partName, ""))
            partName = null;
        if(Objects.equals(supplier, ""))
            supplier =null;
        List<Product> productList = productService.selectByCondition(partName,supplier);
        return ApiResult.success(productList);
    }
}
